package com.zwps.biz.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.enums.FileTypeEnum;
import com.zwps.biz.api.model.vo.fourpipes.PubCommonFileVO;
import com.zwps.biz.api.model.vo.structure.FileCommentVO;
import com.zwps.biz.dal.db.dao.PubCommonFileDAO;
import com.zwps.biz.dal.db.dao.ZTrainRecordDAO;
import com.zwps.biz.dal.db.object.PubCommonFileDO;
import com.zwps.biz.dal.db.object.ZTrainRecordDO;
import com.zwps.biz.domain.service.PubCommonFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@DS("lhk")
@Service
public class PubCommonFileServiceImpl extends ServiceImpl<PubCommonFileDAO, PubCommonFileDO>
    implements PubCommonFileService{
    @Autowired
    ZTrainRecordDAO zTrainRecordDAO;

    @Override
    public List<PubCommonFileVO> getFile() {
        List<PubCommonFileDO> pubCommonFileDOList = this.list(new QueryWrapper<PubCommonFileDO>().eq("file_name", "注册登记"));
        List<PubCommonFileVO> pubCommonFileVOList = pubCommonFileDOList.stream()
                .map(pubCommonFileDO -> BeanUtil.copyProperties(pubCommonFileDO, PubCommonFileVO.class))
                .collect(Collectors.toList());
        return pubCommonFileVOList;
    }

    @Override
    public List<FileCommentVO> getFileList() {
        List<FileCommentVO> fileCommentVOList = new ArrayList<>();
        for (FileTypeEnum value : FileTypeEnum.values()) {
            if (value.getCode()==4){
                FileCommentVO fileCommentVO = new FileCommentVO();
                fileCommentVO.setFileTypeName(value.getDescription());
                List<ZTrainRecordDO> zTrainRecordDOS = zTrainRecordDAO.selectList(new LambdaQueryWrapper<ZTrainRecordDO>().eq(ZTrainRecordDO::getIsDelete, 0).eq(ZTrainRecordDO::getType, 1).orderByDesc(ZTrainRecordDO::getTrainTime));
                List<Object> objectList = new ArrayList<>(zTrainRecordDOS);
                fileCommentVO.setList(objectList);
                fileCommentVO.setFileNums(objectList.size());
                fileCommentVOList.add(fileCommentVO);
            }else {
                FileCommentVO fileCommentVO = new FileCommentVO();
                fileCommentVO.setFileTypeName(value.getDescription());
//                List<PubCommonFileDO> list = this.list(new LambdaQueryWrapper<PubCommonFileDO>()
//                        .eq(PubCommonFileDO::getType, value.getCode())
//                        .orderByDesc(PubCommonFileDO::getCreateTime));
                List<PubCommonFileDO> list = zTrainRecordDAO.getFileList(value.getCode());
                List<Object> collect = list.stream()
                        .map(pubCommonFileDO -> BeanUtil.copyProperties(pubCommonFileDO, PubCommonFileVO.class))
                        .collect(Collectors.toList());
                fileCommentVO.setList(collect);
                fileCommentVO.setFileNums(collect.size());
                fileCommentVOList.add(fileCommentVO);
            }
        }

        return fileCommentVOList;
    }

    @Override
    public List<PubCommonFileDO> pubCommonFileGetList(PubCommonFileDO fileDO) {
        List<PubCommonFileDO> list = this.list(new LambdaQueryWrapper<PubCommonFileDO>()
                .eq(fileDO.getType() != null, PubCommonFileDO::getType, fileDO.getType())
                .orderByDesc(PubCommonFileDO::getCreateTime));
        return list;
    }
}




